package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.adapter.FriendAdapter;
import com.selfdriving.bean.User;
import com.selfdriving.utils.Constant;

public class MyAttentionActivity extends BaseFragmentActivity implements
		OnClickListener
{
	ListView friend_list;
	ImageView topBack;
	TextView topText;
	TextView text;
	private List<User> users;
	private FriendAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_attention);
		initViews();
		initEvents();
		users = new ArrayList<User>();
		for (int i = 0; i < 7; i++)
		{
			User user = new User();
			users.add(user);
		}
		adapter = new FriendAdapter(this, users);
		getDatas();
	}

	@Override
	protected void initViews()
	{
		friend_list = (ListView) findViewById(R.id.friend_list);
		topBack = (ImageView) findViewById(R.id.head_break);
		topText = (TextView) findViewById(R.id.top_text_middle);
		text = (TextView) findViewById(R.id.text);
		topBack.setVisibility(View.VISIBLE);
		topText.setText("我的关注");
	}

	@Override
	protected void initEvents()
	{
		// TODO Auto-generated method stub
		topBack.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.head_break:
			this.finish();
			break;
		}
	}

	public void getDatas()
	{
		showLoadingDialog("数据获取中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "62");
		params.put("phone", mApplication.user.getPhone());
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showShortToast("服务器连接失败");
				dismissLoadingDialog();
				finish();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (json.contains("empty"))
				{
					text.setVisibility(View.VISIBLE);
					dismissLoadingDialog();
					return;
				}
				try
				{
					JSONArray jsonArray = new JSONArray(json);
					friend_list.setAdapter(adapter);
					dismissLoadingDialog();
				} catch (JSONException e)
				{
					showShortToast("数据加载错误");
					finish();
				}
			}
		});
	}
}
